// ==UserScript==
// @name         google-calendar
// @namespace    https://user-web.googlecode.com/
// @version      1
// @description  show task menus
// @include      https://www.google.com/calendar/*
// @include      xhttps://localhost:44300/TestLive.htm
// @version      1
// @grant        none
// ==/UserScript==

try {
    var isTop = window.top == window.self;

    if (isTop) {
        var gc = new function () {
            var me = this;
            log(1);

            me.load = function () {
                me.log(2);
                document.oncontextmenu = function (e) {
                    try {
                        me.log(3);
                        me.showMenus();
                    }
                    catch (ex) {
                        me.error(ex);
                    }
                };
            };

            me.showMenus = function () {
                me.log(4, document.location.href);

                var iframes = document.getElementsByTagName("iframe");
                var iframe = null;

                for (var i = 0; i < iframes.length; i++) {
                    if (iframes[i].src.indexOf("javascript:parent.gtasks.embed.createHtmlForTasksFrame") > -1) {
                        me.log("iframe found!");
                        iframe = iframes[i];
                        break;
                    }
                }

                if (iframe == null) {
                    me.log("iframe not found");
                }
                else {
                    //for dev...
                    // var doc = document.getElementsByTagName("iframe")[2].contentWindow.document;
                    // doc.getElementsByClassName("goog-menu")[2].style.left = doc.body.offsetWidth - 102

                    var doc = iframe.contentWindow.document;
                    var bodyHeight = doc.body.offsetHeight;
                    var bodyWidth = doc.body.offsetWidth;
				
                    var menus = iframe
                        .contentWindow.document
                        .getElementsByClassName("goog-menu");
				
                    var tasks = iframe
                        .contentWindow.document
                        .getElementsByClassName("b");

					var kids = menus[1].children;
					
					for (var i = kids.length - 2; i > -1; i--) {
						kids[i].remove(); //remove all items except for "clear completed"
					}					
						
                    menus[1].style.display = "block";
                    menus[2].style.display = "block";

                    menus[1].style.left = "67px";
                    menus[1].style.top = (bodyHeight - 25) + "px";

                    menus[2].style.left = (bodyWidth - 102) + "px"
                    menus[2].style.top = (bodyHeight - 3 - menus[2].offsetHeight) + "px";

                    /* task width - avoid text going under the menu (this doesn't handle new tasks) */
                    for (var i = 0; i < tasks.length; i++) {
                        tasks[i].style.maxWidth = "310px";
                    }
                }
            };

            me.error = function (ex) {
                var msg = ex.message;

                if (!msg) {
                    for (var i in ex) {
                        msg += ex[i] + " ";
                    }
                }

                me.log("error", msg);
            };

            me.log = function (text1, text2) {
                log(text1, text2);
            };

        };

        window.setTimeout(function () {
            try {
                gc.load();
            }
            catch (ex) {
                log("error", ex.message);
            }
        }, 100);
    }
	else {
		log("skipping", "top: " + window.top.document.location.href + " self: " + window.self.document.location.href);
	}
}
catch (ex) {
    if (ex.message.indexOf("accessing a cross-origin frame") > -1) {
        log("frame", ex.message);
    }
    else {
        log("error", ex.message);
    }
}

function log(text1, text2) {
    if (text2 != null) {
        if (text1.length < 20) {
            text1 = text1 + Array(21 - text1.length).join(" ");
        }

        if (text1.length > 20) {
            text1 = text1.substr(0, 20);
        }

        text1 = text1 + ": " + text2;
    }

    if (text1.length > 100) {
        text1 = text1.substr(0, 100) + "...";
    }

    console.log("[google-calendar] " + text1);
};